package com.csdj.sc.comsumer.controller;

import com.csdj.sc.common.base.BaseController;
import com.csdj.sc.common.base.WebResult;
import com.csdj.sc.common.constant.WebMsgConstant;
import com.csdj.sc.common.entity.User;
import com.csdj.sc.comsumer.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/consumer")
public class TestController extends BaseController {

    @Resource
    private IUserService userService;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/hello")
//    @HystrixCommand(fallbackMethod = "fallback")
    public WebResult test(HttpServletRequest request){
        Locale lang = getLang(request);
        User user= restTemplate.getForObject("http://provider/user/{1}?name={2}",User.class,"cs","cloud");
//        restTemplate.postForObject("http://provider/user",user,String.class);
        return new WebResult(WebMsgConstant.SUCCESS,lang).putData("user",user);
    }

    public WebResult fallback(HttpServletRequest request){
        return new WebResult(WebMsgConstant.FAIL, Locale.CHINA,"错误");
    }


    @GetMapping("/list")
    public WebResult listUser(HttpServletRequest request){
        Enumeration<String> headerNames = request.getHeaderNames();
        List<User> list = userService.list();
        return new WebResult(WebMsgConstant.SUCCESS,list);
    }


    @PostMapping("/update")
    public WebResult update(int id){
        User user = new User();
        user.setId(id);
        User post = userService.post(user);
        return new WebResult(WebMsgConstant.SUCCESS,post);
    }

    @GetMapping("/send")
    public WebResult send(String msg){
        WsController.sendToAll(msg);
        return new WebResult(WebMsgConstant.SUCCESS);
    }
}
